<template>
  <div class="practice-settings-page">
    <PracticeSettings 
      :module="module" 
      @start-practice="onStartPractice"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import PracticeSettings from './PracticeSettings.vue';
import request from '@/api/request';
import { ElMessage } from 'element-plus';

const route = useRoute();
const router = useRouter();

// 从路由参数中获取模块名称，支持props和route.params两种方式
const module = computed(() => route.params.module as string);

// 模块API映射
const moduleApiMap: Record<string, string> = {
  'politics': '/api/politics/practice',
  'commonSense': '/api/commonSense/practice',
  'language': '/api/language/practice',
  'quantitative': '/api/quantity/practice',
  'judgment': '/api/judgment/practice',
  'dataAnalysis': '/api/dataAnalysis/practice',
  'vocabulary-accumulation': '/api/vocabulary/practice',
  'wrongQuestions': '/api/wrong-question/practice'
};

const onStartPractice = async (config: any) => {
  try {
    // 先尝试获取题目数量，检查是否有题目
    const params = config.params || config;
    const apiEndpoint = moduleApiMap[module.value] || '/api/commonSense/practice';
    
    // 特殊处理词汇积累模块和错题本模块，确保正确传递参数
    let requestParams = { ...params };
    if (module.value === 'vocabulary-accumulation') {
      // 确保count参数正确传递
      if (params.count === 0) {
        // 选择全部时传递count=0
        requestParams = { count: 0 };
      } else if (params.count > 0) {
        // 选择固定数量时传递指定数量
        requestParams = { count: params.count };
      }
    } else if (module.value === 'wrongQuestions') {
      // 确保错题本模块的count参数正确传递
      if (params.count === 0) {
        // 选择全部时传递count=0
        requestParams = { count: 0 };
      } else if (params.count > 0) {
        // 选择固定数量时传递指定数量
        requestParams = { count: params.count };
      }
    }
    
    // 发送请求检查是否有题目
    const response = await request.get(apiEndpoint, { params: requestParams });
    
    // 如果没有题目，显示提示并阻止跳转
    if (!response.data || response.data.length === 0) {
      ElMessage.warning('没有找到符合条件的题目，请调整筛选条件后重试');
      return;
    }
    
    // 将参数存储到 sessionStorage，以便 UnifiedPractice 组件可以访问
    sessionStorage.setItem('practiceParams', JSON.stringify(config));
    
    // 根据模块名称导航到对应的练习页面
    switch (module.value) {
      case 'politics':
        router.push('/new-politics-practice');
        break;
      case 'commonSense':
        router.push('/new-common-sense-practice');
        break;
      case 'language':
        router.push('/new-language-practice');
        break;
      case 'quantitative':
        router.push('/new-quantity-practice');
        break;
      case 'judgment':
        router.push('/new-judgment-practice');
        break;
      case 'dataAnalysis':
        router.push('/new-data-analysis-practice');
        break;
      case 'vocabulary-accumulation':
        router.push('/new-vocabulary-accumulation-practice');
        break;
      case 'wrongQuestions':
        router.push('/wrong-questions');
        break;
      default:
        router.push('/');
    }
  } catch (error) {
    console.error('检查题目失败:', error);
    ElMessage.error('检查题目时发生错误，请稍后重试');
  }
};
</script>

<style scoped>
.practice-settings-page {
  width: 100%;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4edf9 100%);
  padding: 24px;
  box-sizing: border-box;
}

@media (max-width: 768px) {
  .practice-settings-page {
    padding: 16px;
  }
}
</style>